<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	#ball{
		width: 50px;
		height: 50px;
		background: red;
		position: absolute;
		display: none;
	}
	</style>
	<script type = "text/javascript" src="jquery-1.11.3.js"></script>
	<script type = "text/javascript" src="jquery.cookie.js"></script>
	<script src = "parabola.js"></script>
	<script>
	$(function(){

		sc_car();

		$.ajax({
			url: "data.json",
			type: "GET",
			success: function(res){
				//将数据进行解析，添加到页面上
				var html = "";
				for(var i = 0; i < res.length; i++){
					html += '<li class="goods_item"  ><div class="goods_pic"><img src="'+res[i].img+'" alt=""></div><div class="goods_title"><p>【京东超市】奥利奥软点小草莓</p></div><div class="sc"><div class="sc_btn" id="'+res[i].id+'" >加入购物车</div></div></li>';
				}
				$(".goods_box ul").html(html);
			}
		})

		//给购物车按钮添加事件
		//页面控件非常多，非常容易叠加，很容易造成事件冒泡
		$(".goods_box").on("click", ".sc_btn", function(){

			//抛物线运动
			ballMove(this);

			// alert(this.id);
			//是否是第一次添加cookie
			var id = this.id;
			var first = $.cookie("goods") == null ? true : false;
			if(first){
				//第一次添加  [{id:id,num:2}]
				$.cookie("goods", '[{id:' + id + ',num:1}]', {
					expires: 7
				});
			}else{
				var str = $.cookie("goods");
				var arr = eval(str);
				var same = false; //代表是否有相同商品

				//遍历所有的对象，判断是否id相同，num++
				for(var i in arr){
					if(arr[i].id == id){
						arr[i].num = arr[i].num + 1;
						var cookieStr = JSON.stringify(arr);
						$.cookie("goods", cookieStr,  {
							expires: 7
						});
						same = true;
						break;
					}
				}

				//没有相同的商品
				if(!same){
					var obj = {id: id, num: 1};
					arr.push(obj);
					var cookieStr = JSON.stringify(arr);
					$.cookie("goods", cookieStr, {
						expires: 7
					});
				}
				sc_car();
			}

			// alert($.cookie("goods"));


			return false;
		})

		function ballMove(node){
			//抛物线运动
			$("#ball").css({
				display: "block",
				left: $(node).offset().left,
				top: $(node).offset().top
			})

			var offsetX = $(".sc_pic").offset().left - $("#ball").offset().left;
			var offsetY = $(".sc_pic").offset().top - $("#ball").offset().top;

			//【注】配置参数的
			var bool = new Parabola({
				el: "#ball",
				targetEl: null,
				offset: [offsetX, offsetY],
				curvature: 0.0005,
				duration: 400,
				callback: function(){
					$("#ball").css("display", "none")
				}
				
			})

			bool.start();
		}
		/*
			mouseenter  移入
			mouseleave  移出
		*/
		$(".sc_right").mouseenter(function(){
			$(this).stop().animate({
				right: 0
			})
			sc_msg();
		});
		$(".sc_right").mouseleave(function(){
			$(this).stop().animate({
				right: -270
			})
		});

		//购物车数字
		function sc_car(){
			var sc_str = $.cookie("goods");
			if(sc_str){ //判断字符串是否存在
				var sc_arr = eval(sc_str);
				var sc_num = 0;
				for(var i in sc_arr){
					sc_num = Number(sc_arr[i].num) + sc_num;
				}
				$(".sc_num").html(sc_num);
			}
		}

		//已经存储在cookie数据进行加载
		function sc_msg(){
			$.ajax({
				url: "data.json",
				type: "get",
				success: function(res){
					var sc_arr = eval($.cookie("goods"));
					var html = '';
					for(var i in sc_arr){
						html += '<li><div class="sc_goodsPic"><img src="'+res[sc_arr[i].id].img+'" alt=""></div><div class="sc_goodsTitle"><p>这是商品曲奇饼干</p></div><div class="sc_goodsBtn" id="'+sc_arr[i].id+'">购买</div><div class="sc_goodsNum">商品数量:'+sc_arr[i].num+'</div></li>';
					}
					$(".sc_right ul").html(html);
				}
			})
		}

	})
	</script>
	<style>
		
		*{
			margin: 0;
			padding: 0;
		}
		body{
			overflow: hidden;
		}
		li{
			list-style-type: none;
		}
		.goods_box ul:after{
			content: "";
			display:block;
			clear: both;
		}
		.goods_box ul{
			width: 1100px;
			margin:0 auto;
		}
		.goods_item{
			padding: 10px 6px;
			border-right: 1px solid #b6b6b6;
			float: left;
			position: relative;
			margin-bottom: 20px;
		}
		.goods_pic{
			padding: 2px 15px;
			margin: 0 auto;
			width: 160px;
			height: 160px;
			transition: all 0.5s ease-in-out;
			-webkit-transition:all 0.5s ease-in-out;
			-moz-transition:all 1s ease-in-out;
		}
		.goods_pic:hover{
			transform:rotate(180deg);
			box-shadow: 2px 2px 5px #b6b6b6;
		}
		.goods_title{
			font-size: 12px;
		    text-align: left;
		    color: #999;
		    padding: 13px 0 7px;
		    display: block;
		    max-width: 100%;
		    _width: 100%;
		    white-space: nowrap;
		    text-overflow: ellipsis;
		    overflow: hidden;
		}
		.sc{
			padding: 10px 5px;
		}
		.sc_btn{
			width: 100px;
			margin: 0 auto;
			text-align: center;
			padding: 5px 10px;
			border-radius: 2px;
			box-shadow: 0 0 2px #f10;
			height: 20px;
			background: #fc1934;
			color: #fff;
			font-weight: bold;
			font-size: 18px;
			cursor:pointer;

		}
		.sc_right{
			position: absolute;
			width: 270px;
			border-left: 2px solid #b6b6b6;
			right: -270px;
			top: 0;
			height: 100%;

		}
		.sc_pic{
			width: 40px;
			height: 40px;
			padding: 4px 6px;
			background: url(toolbars.png) #999 15px 15px no-repeat;
			position: relative;
			left: -52px;
			top: 50%;
			margin-top: -24px;

		}
		.sc_num{
			position: absolute;
			padding: 2px 8px;
			border-radius: 50%;
			background: red;
			color: #fff;
			top: -8px;
			left: 20px;
			z-index: 1;
		}
		.sc_goodsPic{
			
			width: 80px;
			height: 80px;
			border: 1px solid #b6b6b6;
			float: left;

		}
		.sc_goodsPic img{
			width: 100%;
			height: 100%;
		}
		.sc_goodsTitle{
			line-height: 40px;
			color: #999;
			margin-left: 20px;
			float: left;
		}
		.sc_goodsBtn{
			
			background: #fc1934;
			color: #fff;
			text-align: center;
			float: left;
			line-height: 30px;
			width: 60px;
			height: 30px;
			margin-left: 20px;
			cursor: pointer;

		}
		.sc_goodsNum{
			float: left;
			line-height: 30px;
			font-size: 10px;
			margin-left: 5px;

		}
		.sc_right li{
			height: 100px;
		}


	</style>

	<script>
		

	</script>
</head>
<body>
<!-- 商品列表 -->
	<div class="goods_box">
		
		<ul>
			

		</ul>

	</div>

<!-- 右边购物车 -->
	<div class="sc_right">
		<div class="sc_pic">
			<!-- 购物车内商品的数量 -->
			<div class="sc_num">
				0
			</div>
		</div>

		<ul>
			
		</ul>
	</div>
	<div id = "ball"></div>
	
</body>
</html







